package top.hidoctor.auscultation.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tang.util.GsonUtil;

import java.util.ArrayList;
import java.util.List;

import top.hidoctor.auscultation.R;
import top.hidoctor.auscultation.adapter.DoctorAdapter;
import top.hidoctor.auscultation.bean.MarkDetailBean;

public class ChooseDocPop extends PopupWindow {


    private final Button btn;
    public RecyclerView recyclerView;

    private View mMenuView;

    private Activity mContext;

    private DoctorAdapter docAdapter;
    private List<MarkDetailBean> chooseList = new ArrayList<>();

    public List<MarkDetailBean> getChooseList() {
        chooseList.clear();
        for (int i = 0; i < docAdapter.getData().size(); i++) {
            if (docAdapter.getData().get(i).isCheck()) {
                chooseList.add(docAdapter.getData().get(i));
            }
        }
        return chooseList;
    }

    public ChooseDocPop(final Activity context, final View.OnClickListener onClickListener) {

        super(context);

        this.mContext = context;

        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        mMenuView = inflater.inflate(R.layout.popu_choose_doc, null);
        mMenuView.findViewById(R.id.viewRoot).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        recyclerView = (RecyclerView) mMenuView.findViewById(R.id.recycler);
        btn = (Button) mMenuView.findViewById(R.id.btn);
        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 5));
        docAdapter = new DoctorAdapter(R.layout.item_doctor, null);
        recyclerView.setAdapter(docAdapter);
        // 设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        // 设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.popwin_anim_style);
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x66000000);
        // 设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);

        docAdapter.setOnRecyclerViewItemChildClickListener(new BaseQuickAdapter.OnRecyclerViewItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                docAdapter.getData().get(position).setCheck(!docAdapter.getData().get(position).isCheck());
                docAdapter.notifyItemChanged(position);
            }
        });

        btn.setOnClickListener(onClickListener);
    }


    public void setData(List<MarkDetailBean> list) {

        docAdapter.setNewData(list);
    }

    @Override
    public void showAsDropDown(View anchor) {
        if (Build.VERSION.SDK_INT >= 24) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor);
    }
}
